#ifndef FILETRANSFARECON_H_
#define FILETRANSFARECON_H_


#include <wx/socket.h>
#include "PackageUtils.h"

namespace Impp
{

class FileTransfareFrame
{
public:
	char magic[4];        /* 0 */
	word length; 		  /* 4 */
	word type;            /* 6 */
	qword cookie;     /* 8 */
	word encrypt;         /* 16 */
	word compress;        /* 18 */
	word totfiles;        /* 20 */
	word filesleft;       /* 22 */
	word totparts;        /* 24 */
	word partsleft;       /* 26 */
	dword totsize;         /* 28 */
	dword size;            /* 32 */
	dword modtime;         /* 36 */
	dword checksum;        /* 40 */
	dword rfrcsum;         /* 44 */
	dword rfsize;          /* 48 */
	dword cretime;         /* 52 */
	dword rfcsum;          /* 56 */
	dword nrecvd;          /* 60 */
	dword recvcsum;        /* 64 */
	char idstring[32];     /* 68 */
	word flags;            /* 100 */
	word lnameoffset;      /* 101 */
	word lsizeoffset;      /* 102 */
	char dummy[69];        /* 103 */
	char macfileinfo[16];  /* 172 */
	word nencode;          /* 188 */
	word nlanguage;        /* 190 */
	char *name;            /* 192 */
	size_t name_length;
	/* Payload? */           /* 256 */
};

class FrameBuffer{
public:	
	FrameBuffer() :
		pos(0){}
	
	size_t pos;
	byte buffer[256];
};

class FileTransfareCon : private wxEvtHandler
{
private:
	wxSocketClient* client;
	wxSocketClient* clientTryIntern;
	wxSocketClient* clientTryExtern;
	enum
	{
		ID_SOCKET_EXTERN = 200,
		ID_SOCKET_INTERN = 201
	};
	
	FrameBuffer buffer;
	
public:
	FileTransfareCon();
	virtual ~FileTransfareCon();
	
	bool connect(const FileTransfareInfo & info);
	void onSocketEvent(wxSocketEvent & event);
	
	FileTransfareFrame* readFrame();
	
private:
	bool read(size_t lng);

private:
    DECLARE_EVENT_TABLE()
};

}

#endif /*FILETRANSFARECON_H_*/
